package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> dishList = dishMapper.list(categoryId);
        return dishList;
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //先查询菜品
        Dish dish = dishMapper.getById(id);
        //再查询口味
        List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(id);
        //组装DishVO对象
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        //获得分类名称
        Category category = categoryMapper.getById(dish.getCategoryId());
        dishVO.setCategoryName(category.getName());
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional//设计多张表操作，开始事务管理
    @Override
    public void add(DishDTO dishDTO) {
        //先将菜品基本数据插入dish表中
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        dish.setCreateTime(LocalDateTime.now());
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setUpdateUser(BaseContext.getCurrentId());
//        dish.setCreateUser(BaseContext.getCurrentId());
        dishMapper.insert(dish);
        Long dishID = dish.getId();
        //再将菜品口味数据批量插入dish_flavor中
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors!=null&&flavors.size()!=0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishID);
            });
            //批量插入
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional//开启事务管理
    public void update(DishDTO dishDTO) {
        //先更新菜品表基本数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        dish.setUpdateUser(BaseContext.getCurrentId());
//        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dish);
        //再更新菜品口味表数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //先将原来的口味数据删除，再进行插入
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //批量插入
        if (flavors!=null&&flavors.size()!=0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 启售停售菜品
     * @param status
     */
    @Override
    public void startOrStop(Integer status,Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.startOrStop(dish);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteBatchByDishId(ids);
    }
}
